package cn.jingyuan.owl.web.oauth2;

import cn.jingyuan.bee.fastjson.FastJsonUtils;
import cn.jingyuan.bee.utils.bean.BeanUtils;
import cn.jingyuan.swan.cloud.core.Result;
import org.springframework.boot.autoconfigure.security.oauth2.resource.AuthoritiesExtractor;
import org.springframework.security.core.GrantedAuthority;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class DefaultAuthoritiesExtractor implements AuthoritiesExtractor {

    @Override
    public List<GrantedAuthority> extractAuthorities(Map<String, Object> map) {
        Result<Map<String, Object>> result = BeanUtils.mapToBean(map, Result.class, false);
        Map<String, Object> userInfoMap = result.getData();

        Collection<? extends GrantedAuthority> extractAuthorities = AuthenticationPrincipalHelper.extractAuthorities(userInfoMap);

        List<GrantedAuthority> authorities = new ArrayList<>(extractAuthorities);

        return authorities;
    }

}
